package com.nintendo.bubblechat.service.impl;

import com.nintendo.bubblechat.config.MinioConfig;
import com.nintendo.bubblechat.entity.dto.CircleOfFriendsDTO;
import com.nintendo.bubblechat.entity.pojo.*;
import com.nintendo.bubblechat.exceptions.GraceException;
import com.nintendo.bubblechat.grace.result.GraceJsonRequest;
import com.nintendo.bubblechat.grace.result.ResponseStatusEnum;
import com.nintendo.bubblechat.mapper.CircleForeignKeyMapper;
import com.nintendo.bubblechat.mapper.CircleOfFriendsMapper;
import com.nintendo.bubblechat.service.CircleOfFriendsService;
import com.nintendo.bubblechat.utils.MinioUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class CircleOfFriendsImpl implements CircleOfFriendsService {
    @Autowired
    private CircleOfFriendsMapper circleOfFriendsMapper;
    @Autowired
    private MinioConfig minioConfig;
    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private Sid sid;
    @Autowired
    private CircleForeignKeyMapper circleForeignKeyMapper;
    @Override
    public void createUp(CircleOfFriendsDTO circleOfFriendsDTO) {
        CircleOfFriends circleOfFriends = new CircleOfFriends();
        List<String> imageUrlByCircleId = findImageUrlByCircleId(circleOfFriendsDTO.getCircleId());
        circleOfFriends.setCreateUserFace(circleOfFriendsDTO.getCreateUserFace());
        circleOfFriends.setCreateUserId(circleOfFriendsDTO.getCreateUserId());
        circleOfFriends.setCreateUserNickname(circleOfFriendsDTO.getCreateUserNickname());
        circleOfFriends.setId(sid.nextShort());
        circleOfFriends.setCreateTime(new Date());
        circleOfFriends.setTitle(circleOfFriendsDTO.getTitle());
        circleOfFriends.setResourceUrl(imageUrlByCircleId.toString());

        circleOfFriendsMapper.insert(circleOfFriends);
    }

    @Override
    public String uploadFile(MultipartFile file,String userId,String circleId) {
        try {
            String url = minioUtils.upload(file);
            url = "http://"+minioConfig.getEndpoint()+":"+minioConfig.getPort()+"/"+url;
            CircleForeignKey circleForeignKey = new CircleForeignKey(sid.nextShort(),userId,url,circleId);
            circleForeignKeyMapper.insertCircleForeignKey(circleForeignKey);
            return circleId;
        } catch (Exception e) {
            e.printStackTrace();
        }
        GraceException.display(ResponseStatusEnum.FILE_UPLOAD_FAILD);
        return null;
    }

    @Override
    public List<CircleOfFriends> findCircleOfFriendsBy() {
        return circleOfFriendsMapper.findCircleOfFriendBy();
    }

    @Override
    public List<CircleOfFriends> findMyCircleOfFriends(String myId) {
        List<CircleOfFriends> circleOfFriends =  circleOfFriendsMapper.findCircleOfFriendByMyUserId(myId);
        for (CircleOfFriends c :circleOfFriends){
            List<Users> thumbUsers = findThumbUsers(c.getId());
            List<CircleComment> circleComments = findCircleComment(c.getId());
            c.setThumbUsers(thumbUsers);
            c.setCircleComments(circleComments);
        }

        return circleOfFriends;
    }
    private List<CircleComment> findCircleComment(String id){
        return circleOfFriendsMapper.findCircleComment(id);
    }
    private List<Users> findThumbUsers(String id){
        return circleOfFriendsMapper.findThumbUsers(id);
    }
    @Override
    public void thumbCircle(String circleId, String userId) {
        ThumbCircle thumbCircle = new ThumbCircle();
        thumbCircle.setId(sid.nextShort());
        thumbCircle.setCircleId(circleId);
        thumbCircle.setUserId(userId);
        circleOfFriendsMapper.thumbCircle(thumbCircle);
    }

    @Override
    public void cancelThumbCircle(String circleId, String userId) {
        circleOfFriendsMapper.cancelThumbCircle(circleId,userId);
    }

    @Override
    public void bindComment(CircleComment circleComment) {
        circleComment.setId(sid.nextShort());
        circleOfFriendsMapper.bindCircleComment(circleComment);
    }

    /**
     * 根据朋友圈Id查询所有图片
     */
    public List<String> findImageUrlByCircleId(String circleId){
        List<String> imageUrls = circleForeignKeyMapper.findImageUrlByCircleId(circleId);
        if (imageUrls.size() == 0){
            return Collections.emptyList();
        }
        return imageUrls;
    }
}
